/*
 * passwordValidationDialog.java
 *
 * Created on __DATE__, __TIME__
 */

package serverPresentation;

import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import connection.exception.CardErrorException;

import server.ServerData;

/**
 *
 * @author  __USER__
 */
public class passwordValidationDialog extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;

	private String _nextForm;

	/** Creates new form passwordValidationDialog */
	public passwordValidationDialog(String nextForm) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		initComponents();

		Image im = Toolkit.getDefaultToolkit().getImage(
		"images/security-icon.gif");
		this.setIconImage(im);

		_nextForm = nextForm;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		nextButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();
		passwordBox = new javax.swing.JPasswordField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setResizable(false);

		jLabel1.setText("Introduza a Password para Continuar a opera\u00e7\u00e3o:");

		nextButton.setText("Continuar >");
		nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				nextButtonMouseClicked(evt);
			}
		});

		cancelButton.setText("Cancelar");
		cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				cancelButtonMouseClicked(evt);
			}
		});

		passwordBox.setText("passwordBox");
		passwordBox.setFocusCycleRoot(true);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
						.addContainerGap(151, Short.MAX_VALUE)
						.addComponent(nextButton).addGap(18, 18, 18)
						.addComponent(cancelButton).addContainerGap())
						.addGroup(
								layout.createSequentialGroup().addGap(48, 48, 48)
								.addComponent(jLabel1)
								.addContainerGap(51, Short.MAX_VALUE))
								.addGroup(
										layout.createSequentialGroup()
										.addGap(120, 120, 120)
										.addComponent(passwordBox,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												102,
												javax.swing.GroupLayout.PREFERRED_SIZE)
												.addContainerGap(135, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jLabel1)
						.addGap(38, 38, 38)
						.addComponent(passwordBox,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										46, Short.MAX_VALUE)
										.addGroup(
												layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(nextButton)
														.addComponent(cancelButton))
														.addContainerGap()));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void nextButtonMouseClicked(java.awt.event.MouseEvent evt) {
		char[] password = passwordBox.getPassword();

		if (password.length == 0) {
			JOptionPane.showMessageDialog(this,
			"A password não pode ser vazia!!");
			return;
		}

		boolean validPassword = ServerData.getServerData().getKeystoreManager()
		.validatePassword(password);

		if (!validPassword) {
			JOptionPane.showMessageDialog(this,
			"A password introduzida não é válida!!");
			return;
		}

		if (_nextForm.equals("AddUserCompetenceMenu")) {
			this.setVisible(false);
			AddUserCompetenceMenu competenceMenu = new AddUserCompetenceMenu();
			competenceMenu.setVisible(true);
			this.dispose();
		} else if (_nextForm.equals("AddUserWithCcMenu")) {
			this.setVisible(false);
			try{
				AddUserWithCcMenu ccMenu = new AddUserWithCcMenu();
				ccMenu.setVisible(true);
				this.dispose();
			}catch(CardErrorException e){
				JOptionPane.showMessageDialog(this, "O cartão não está inserido no leitor!!");
				return;
			}
		} else {
			this.setVisible(false);
			AddUserWithPasswordMenu passMenu = new AddUserWithPasswordMenu();
			passMenu.setVisible(true);
			this.dispose();
		}
	}

	private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {
		this.setVisible(false);
		this.dispose();
		ServerData.getServerData().getMainMenu().setVisible(true);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new passwordValidationDialog("").setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton cancelButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JButton nextButton;
	private javax.swing.JPasswordField passwordBox;
	// End of variables declaration//GEN-END:variables

}